package com.itheima.config;

import com.itheima.filter.JwtVerifyFilter;
import com.itheima.filter.TokenLoginFilter;
import com.itheima.filter.TokenVerifyFilter;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private RsaKeyProperties prop;

    @Autowired
    private UserService myCustomUserService;

    @Bean
    public BCryptPasswordEncoder myPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }

    //SpringSecurity配置信息
//    public void configure(HttpSecurity http) throws Exception {
//        http.csrf()
//            .disable()
//            .authorizeRequests()
//            .antMatchers("/product").hasAnyRole("USER")
//            .anyRequest()
//            .authenticated()
//            .and()
//            .addFilter(new JwtVerifyFilter(super.authenticationManager(), prop))
//            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
//    }

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        //UserDetailsService类
        auth.userDetailsService(myCustomUserService)
                //加密策略
                .passwordEncoder(myPasswordEncoder());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                //关闭跨站请求防护
                .cors().and().csrf().disable()
                //允许不登陆就可以访问的方法，多个用逗号分隔
                .authorizeRequests().antMatchers("/product").hasAnyRole("USER")
                //其他的需要授权后访问
                .anyRequest().authenticated().and()
                //增加自定义认证过滤器
                .addFilter(new TokenLoginFilter(authenticationManager(), prop))
                //增加自定义验证认证过滤器
                .addFilter(new TokenVerifyFilter(authenticationManager(), prop));

        // 前后端分离是无状态的，不用session了，直接禁用。
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);

    }
}
